import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Menu implements Runnable {
	JFrame frame;
	boolean running;
	Image background = ImageLoader.images.get("mainMenu");
	MyButton aboutButton, cardsButton, instructionButton, playButton,
			quitButton, menuButton;

	public Menu() {
		createFrame();

		playButton = new MyButton(226, 310,
				ImageLoader.images.get("playButton"));
		aboutButton = new MyButton(226, 355,
				ImageLoader.images.get("aboutButton"));
		instructionButton = new MyButton(226, 400,
				ImageLoader.images.get("instructionButton"));
		cardsButton = new MyButton(226, 444,
				ImageLoader.images.get("cardsButton"));
		quitButton = new MyButton(398, 444,
				ImageLoader.images.get("quitButton"));
		menuButton = new MyButton(619, 575,
				ImageLoader.images.get("littleMenuButton"));
	}

	public void createFrame() {
		frame = new JFrame();
		frame.setSize(800, 640);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setUndecorated(true);
		frame.addMouseListener(new MouseListen());
		frame.addKeyListener(new KeyListen());
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setIgnoreRepaint(true);
		frame.createBufferStrategy(2);
	}

	public void run() {
		running = true;
		while (running) {
			draw();

			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
			}
		}

		frame.dispose();
		new Game().run();
	}

	public void draw() {
		BufferStrategy strategy = frame.getBufferStrategy();
		Graphics g = strategy.getDrawGraphics();

		g.drawImage(background, 0, 0, null);
		if (background == ImageLoader.images.get("mainMenu")) {
			playButton.draw(g);
			aboutButton.draw(g);
			instructionButton.draw(g);
			cardsButton.draw(g);
			quitButton.draw(g);
		} else {
			menuButton.draw(g);
		}

		g.dispose();

		if (!strategy.contentsLost()) {
			strategy.show();
		}
	}

	public static void main(String[] args) {
		Menu game = new Menu();
		game.run();
	}

	class MouseListen extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			Point p = new Point(e.getX(), e.getY());

			if (background == ImageLoader.images.get("mainMenu")) {
				if (playButton.contains(p)) {
					running = false;
				} else if (quitButton.contains(p)) {
					System.exit(0);
				} else if (cardsButton.contains(p)) {
					background = ImageLoader.images.get("cardsMenu");
				} else if (aboutButton.contains(p)) {
					background = ImageLoader.images.get("aboutMenu");
				} else if (instructionButton.contains(p)) {
					background = ImageLoader.images.get("instructionsMenu");
				}
			} else {
				if (menuButton.contains(p)) {
					background = ImageLoader.images.get("mainMenu");
				}
			}

		}
	}

	class KeyListen extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
				System.exit(0);
			}
		}
	}
}
